<?php
namespace app\newapi\controller;

use think\Controller;
use app\common\model\OnlineAsk as OnlineAskModel;
use app\common\model\OnlineUsers as OnlineUsersModel;
use app\common\model\OnlineMessage as OnlineMessageModel;

class OnlineMessage extends Controller
{
    protected $online_message_model;
    protected $online_users_model;
    protected $online_ask_model;

    protected function _initialize(){
        parent::_initialize();
        $this->online_users_model = new OnlineUsersModel;
        $this->online_message_model = new OnlineMessageModel;
        $this->online_ask_model = new OnlineAskModel;
    }

    public function getOnlineMsgDetail($mid){
        $online_msg_info = $this->online_ask_model->get($mid);
        if($online_msg_info){
            $online_msg_info['speech'] = html_entity_decode($online_msg_info['speech']);
            return json(['status' => 1 , 'msg' => 'ok' , 'online_msg_info' => $online_msg_info]);
        }else{
            return json(['status' => -1 , 'msg' => 'error']);
        }
    }

    public function saveMessage(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = $this->online_message_model->allowField(true)->save($data);
            if($result){
                return json(['status' => 1 , 'msg' => 'ok']);
            }else{
                return json(['status' => -1 , 'error' => 'save error']);
            }
        }
    }

    public function getUserList($cs_id) {
        $user_list = $this->online_users_model
                ->alias('u')
                ->field('u.id,u.adv_id,u.create_time,u.user_ip,m.send_time')
                ->join('think_online_message m','m.user_id = u.id')
                ->where('u.butt_id',$cs_id)
                ->order('m.send_time DESC')
                ->group('user_id')
                ->limit(30)
                ->select();
        return json(['status' => 1 , 'msg' => 'ok' , 'user_list' => $user_list]);
    }

    public function getUserInfo($id) {
        $userInfo = $this->online_users_model->where('id',$id)->find();
        if($userInfo){
            $userInfo['visit'] = unserialize($userInfo['visit']); 
            $history_msg = $this->online_message_model->field('cs_id,message,type,send_type,send_time')->where('user_id',$id)->select();
            $userInfo['chat_list'] = $history_msg;
            return json(['status' => 1 , 'msg' => 'ok' , 'userinfo' => $userInfo]);
        } 
        return json(['status' => -1 , 'error' => 'no userinfo']);
    }
    
    /**
     * 储存用户信息
     */
    public function saveUserInfo(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($user_info = $this->online_users_model->where('user_ip',$data['user_ip'])->find()){
                $visit = unserialize($user_info['visit']);
                $visit[] = time();
                $result = $this->online_users_model->allowField(true)->save(['visit' => serialize($visit)],['user_ip' => $data['user_ip']]);
                unset($user_info['visit']);
            }else{
                $user_info = $data;
                //adv_id
                $butt = $this->online_ask_model->alias('o')->field('c.id')->join('think_csuser c','o.auid = c.auid')->where('o.id',$data['adv_id'])->where('c.status',1)->where('type',1)->column('c.id,c.today_butt');
                $butt_id = $this->getMinButt($butt);

                $user_info['butt_id'] = $butt_id;
                
                $online_butt = M('csuser')->execute("UPDATE `think_csuser`  SET `today_butt`= $butt[$butt_id] + 1,`online_butt`= online_butt + 1 WHERE  `id` = $butt_id");//修改客服在线对接数

                $user_info['create_time'] = time();
                $result = $this->online_users_model->saveUser($user_info);
            }
            
            if($result){
                return json(['status' => 1 , 'msg' => 'ok' , 'user_info' => $user_info]);
            }else{
                return json(['status' => -1 , 'error' => 'save error']);
            }
        }
    }

    /**
     * 获取对接数最少的客服
     */
    private function getMinButt($butt){
        $min_acc = min($butt);
        foreach($butt as $ko=>$vo){
            if($vo == $min_acc){
                $butt_id = $ko; //获取对接最少客服id
                break;
            }
        }
        return $butt_id;
    }
}